home *** CD-ROM | disk | FTP | other *** search
- /*
- * mail check control panel
- * by Aaron Wohl (aw0g+@andrew.cmu.edu) jul 1990
- * Carnegie-Mellon University
- * Special Projects
- * Pittsburgh, PA 15213-3890
- * (412)-268-5032
- */
-
- #include <cdev.h>
- #include "mmc_core.h"
- #include "gms.h"
- #include "mmc_drvr_install.h"
- #include "mmc_drvr_find.h"
- #include "string.h"
- #include "mmc_prep.h"
- #include "mmc_os_preserve.h"
-
- /*
- * allocate some vars with newptr so they don't float around and
- * can be passed to system calls
- */
- struct fs_t_R {
- TEHandle hTE; /*text for user to check*/
- Rect error_box; /*box around error text*/
- PicHandle arrows; /*picture of an up and down arrow*/
- Rect arrow_box;
- Rect remind_box;
- Rect sound_box;
- long old_status; /*driver status word*/
- Str255 sound_text;
-
- int ref_num;
- mmc_state cstate;
- long last_state_update;
- int init_done;
- };
- typedef struct fs_t_R fs_t,*fs_t_pt;
-
- struct mailcheck : cdev {
- fs_t *fs; /*fixed allocated stuff*/
- void compute_mmc(void);
- void find_driver(void);
- void set_display(void);
- void set_check_box(int item,int setting);
- void init_username_text(void);
- void set_username_text(void);
- void set_error_text(void);
- void new_error(char *text);
- int read_current_state_from_driver(mmc_state_pt s);
- int really_read_current_state_from_driver(mmc_state_pt s);
- int write_current_state_to_driver(mmc_state_pt s);
- void applybutton(void);
- void find_driver_state(void);
- void usernametocur(void);
- void init_arrows(void);
- void set_remind_display(void);
- void adjust_remind(long delta);
- long figure_step(long direction);
- void working_SetIText(Handle text,Str255 buf,Rect r);
- void set_item_hilite(int item,int setting);
- void set_status_text(char *new_text);
- void set_new_status(long newst);
- void check_new_status(long new_st);
- void choose_sound(void);
- void set_sound_display(void);
- void set_sound(MenuHandle sound_pop,int pick);
- void snooze(void);
- int get_sound_menuid(MenuHandle sound_pop);
-
- void Init(void); /* "initDev" */
- void Close(void); /* "closeDev" */
- void Activate(void); /* "activDev" */
- void Update(void); /* "updateDev" */
- void Idle(void); /* "nulDev" */
- void ItemHit(short); /* "hitDev" */
- void Key(short); /* "keyEvtDev" */
- void CmdKey(short); /* "keyEvtDev" (command) */
- void Deactivate(void); /* "deactiveDev" */
- void Cut(void); /* "cutDev" */
- void Copy(void); /* "copyDev" */
- void Paste(void); /* "pasteDev" */
- void Clear(void); /* "clearDev" */
- };
-
-
- enum {
- fn_user_name=1,
- fn_enable_mail_checkbox=3,
- fn_init_status_text=4,
- fn_apply_button=5,
- fn_last_user_checkbox=6,
- fn_driver_status=7,
- fn_remind_time=8,
- fn_more_remind=9,
- fn_less_remind=10,
- fn_note=13,
- fn_blink=14,
- fn_snooze=15,
- fn_sound=17,
- fn_enable_sound=18
- };
-
-
- /*
- * Runnable - should the cdev appear in the Control Panel?
- *
- * This implements the "macDev" message.
- *
- */
- Boolean Runnable()
- {
- return true;
- }
-
-
- /*
- * New - create the cdev object
- *
- */
- cdev *New()
- {
- return(new(mailcheck));
- }
-
-
- void mailcheck::find_driver()
- {
- fs->ref_num=mmc_drvr_find(MMC_name,0L);
- }
-
- /*
- * set a checkbox as it is displayed
- */
- void mailcheck::set_item_hilite(int item,int setting)
- {
- short type;
- Handle ctl;
- Rect box;
- GetDItem((WindowPtr)dp, lastItem + item, &type, &ctl, &box);
- HiliteControl((ControlHandle)ctl,(setting)?1:0);
- }
-
- /*
- * set a checkbox as it is displayed
- */
- void mailcheck::set_check_box(int item,int setting)
- {
- short type;
- Handle ctl;
- Rect box;
- Str255 buf;
- GetDItem((WindowPtr)dp, lastItem + item, &type, &ctl, &box);
- SetCtlValue((ControlHandle)ctl,(setting)?1:0);
- }
-
- void mailcheck::set_username_text(void)
- {
- int type;
- Handle text;
- Rect box;
- Str255 buf;
- int len=strlen(fs->cstate.mmc_uname);
- len=imin(254,len);
- if(len!=0)
- memcpy(&buf[1],fs->cstate.mmc_uname,len);
- buf[0]=len;
- TESetText(&buf[1], buf[0],fs->hTE);
- }
-
- /*
- * initialize arrow drawing code
- */
- void mailcheck::init_arrows()
- {
- short type;
- Handle user_item_handle;
- Rect upbox;
- int dx;
- int dy;
- GetDItem((WindowPtr)dp, lastItem + fn_more_remind, &type, &user_item_handle, &upbox);
- fs->arrows=(PicHandle)GetNamedResource('PICT',"\pRemindArrows");
- fs->arrow_box=(*(fs->arrows))->picFrame;
- dx=fs->arrow_box.left-upbox.left;
- dy=fs->arrow_box.top-upbox.top;
- fs->arrow_box.left=upbox.left;
- fs->arrow_box.top=upbox.top;
- fs->arrow_box.right-=dx;
- fs->arrow_box.bottom-=dy;
-
- fs->error_box=dp->port.portRect;
- fs->remind_box=dp->port.portRect;
- fs->sound_box=dp->port.portRect;
- }
-
- /*
- * set the username text
- */
- void mailcheck::init_username_text()
- {
- short type;
- Handle text;
- Rect box;
- Str255 buf;
- GetDItem((WindowPtr)dp, lastItem + fn_user_name, &type, &text, &box);
- GetIText(text, buf);
- SetDItem((WindowPtr)dp, lastItem + fn_user_name, userItem, 0, &box);
- InsetRect(&box, 2, 2);
- fs->hTE = TENew(&box, &box);
- TEAutoView(true, fs->hTE);
-
- TESetText(&buf[1], buf[0], fs->hTE);
- TESetSelect(0, 32767, fs->hTE);
- TEActivate(fs->hTE);
- }
-
- /*
- * SetIText doesn't seem to invalidate....
- */
- void mailcheck::working_SetIText(Handle text,Str255 buf,Rect r)
- {
- GrafPtr old_port;
- GetPort(&old_port);
- SetPort(&dp->port);
- SetIText(text,buf);
- InvalRect(&r);
- SetPort(old_port);
- }
-
- void mailcheck::set_status_text(char *new_text)
- {
- short type;
- Handle text;
- Rect box;
- Str255 buf;
- int len=strlen(new_text);
- len=imin(254,len);
- GetDItem((WindowPtr)dp, lastItem + fn_driver_status, &type, &text, &box);
-
- if(len!=0)
- memcpy(&buf[1],new_text,len);
- buf[0]=len;
- working_SetIText(text,buf,box);
- }
-
- LDEF(char *name_bit(long bit))
- {
- switch(bit) {
- case MCS_snooze:
- return "snooze";
- case MCS_want_open:
- return "please_open";
- case MCS_hearmm:
- return "trackmm";
- case MCS_io_pending:
- return "io";
- case MCS_debug:
- return "debug";
- case MCS_nsound:
- return "sound";
- case MCS_nblink:
- return "blink";
- case MCS_npost:
- return "post";
- default:
- return "?";
- }
- }
-
-
- LDEF(print_flags(char *buf,long aflag))
- {
- char *sep="";
- long i;
- for(i=1;i!=0;i<<=1)
- if((i&aflag)!=0) {
- strcat(buf,sep);
- strcat(buf,name_bit(i));
- sep=", ";
- }
- }
-
- void mailcheck::set_new_status(long newst)
- {
- char buf[300];
- fs->old_status=newst;
- buf[0]=0;
- if((newst== -1)||
- (newst== -2))
- strcat(buf,"Driver not installed, enable, apply and reboot to start it.");
- else if(newst== 0)
- strcat(buf,"Driver is off");
- else
- print_flags(buf,newst);
- set_status_text(buf);
-
- }
-
- void mailcheck::check_new_status(long newst)
- {
- if(newst== fs->old_status)
- return;
- set_new_status(newst);
- }
-
- void mailcheck::set_error_text()
- {
- short type;
- Handle text;
- Rect box;
- Str255 buf;
- int len=strlen(fs->cstate.mmc_text_state);
- len=imin(254,len);
- GetDItem((WindowPtr)dp, lastItem + fn_init_status_text, &type, &text, &box);
-
- if(len!=0)
- memcpy(&buf[1],fs->cstate.mmc_text_state,len);
- buf[0]=len;
- working_SetIText(text,buf,box);
- InsetRect(&box,-2,-2);
- fs->error_box=box;
- }
-
- void mailcheck::new_error(char *text)
- {
- if(strcmp(text,fs->cstate.mmc_text_state)==0)
- return;
- strcpy(fs->cstate.mmc_text_state,text);
- set_error_text();
- }
-
- #define CUR_MINS ((fs->cstate.mmc_remind_time+MINS-1)/MINS)
-
- /*
- * set the text displayed in the remind info
- */
- void mailcheck::set_remind_display()
- {
- short type;
- Handle text;
- Rect box;
- Str255 buf;
- NumToString(CUR_MINS,buf);
- GetDItem((WindowPtr)dp, lastItem + fn_remind_time, &type, &text, &box);
- working_SetIText(text,buf,box);
- InsetRect(&box,-2,-1);
- fs->remind_box=box;
- }
-
- /*
- * set the text displayed in the remind info
- */
- LDEF(long set_sound_display_get(int id,unsigned char *sound_text))
- {
- Handle res_han;
- short res_id;
- ResType res_type;
- sound_res_state cur_res;
- cur_res=mmc_use_sound_res();
- res_han=GetResource('snd ',id);
- if(res_han==0)
- memcpy(sound_text,"\pSound?",10);
- else
- GetResInfo(res_han,&res_id,&res_type,sound_text);
- mmc_close_sound_res(cur_res);
- }
-
- void mailcheck::set_sound_display()
- {
- short type;
- Handle text;
- Rect box;
- Rect inv_rect;
-
- GetDItem((WindowPtr)dp, lastItem + fn_sound, &type, &text, &box);
- fs->sound_box=box;
-
- OSP_protected_call(OSP_noload,set_sound_display_get,fs->cstate.mmc_sound_id,fs->sound_text);
- inv_rect=fs->sound_box;
- InsetRect(&inv_rect,1,1);
- InvalRect(&inv_rect);
- }
-
- /*
- * see how big a step one click should do
- */
- long mailcheck::figure_step(long direction)
- {
- long result=5; /*defult is 5min step*/
- if(direction<0) {
- if(CUR_MINS<=15)
- result=1;
- } else if(CUR_MINS<15)
- result=1;
- return result*direction;
- }
-
- /*
- * increase or decrease remind time
- */
- void mailcheck::adjust_remind(long delta)
- {
- long new_time=fs->cstate.mmc_remind_time+delta*MINS;
- new_time=imax(0,new_time);
- new_time=imin(new_time,999L*MINS);
- if(new_time==fs->cstate.mmc_remind_time)
- return;
- fs->cstate.mmc_remind_time=new_time;
- set_remind_display();
- }
-
- /*
- * set the user interface to match fs.cstate
- */
- void mailcheck::set_display()
- {
- set_check_box(fn_enable_mail_checkbox,FLSET(fs->cstate,MCS_want_open));
- set_check_box(fn_last_user_checkbox,FLSET(fs->cstate,MCS_hearmm));
- set_check_box(fn_note,FLSET(fs->cstate,MCS_npost));
- set_check_box(fn_blink,FLSET(fs->cstate,MCS_nblink));
- set_check_box(fn_enable_sound,FLSET(fs->cstate,MCS_nsound));
- set_username_text();
- set_remind_display();
- set_error_text();
- set_sound_display();
- }
-
- int mailcheck::really_read_current_state_from_driver(mmc_state_pt s)
- {
- int err;
- if(fs->ref_num==0)
- return FALSE;
- err=mmc_get_drvr_state(fs->ref_num,s);
- if(err!=0) {
- strcpy(s->mmc_text_state,"Can't read_driver state");
- return FALSE;
- }
- return TRUE;
- }
-
- int mailcheck::read_current_state_from_driver(mmc_state_pt s)
- {
- int err=really_read_current_state_from_driver(s);
- if(!err)
- check_new_status(-1);
- else
- check_new_status(s->mmc_st);
- return err;
- }
-
- void mailcheck::find_driver_state()
- {
- mmc_state s;
- if(read_current_state_from_driver(&s))
- new_error(s.mmc_text_state);
- }
-
-
- int mailcheck::write_current_state_to_driver(mmc_state_pt s)
- {
- int err;
- err=mmc_set_drvr_state_sound_yes(fs->ref_num,s,MMC_set_state);
- set_error_text();
- return err;
- }
-
- void mailcheck::usernametocur()
- {
- char **text=(*fs->hTE)->hText;
- int len;
- len=imin(MMC_uname_len-1,GetHandleSize(text));
- fs->cstate.mmc_uname[0]=0;
- if(len!=0) {
- memcpy(fs->cstate.mmc_uname,*text,len);
- fs->cstate.mmc_uname[len]=0;
- }
- }
-
- void mailcheck::CmdKey(c)
- short c;
- {
- switch(c) {
- case 'd': case 'D':
- fs->cstate.mmc_st^=MCS_debug;
- break;
- default:
- inherited::CmdKey(c);
- }
- }
-
- void tick(int refnum);
- void tick(int refnum)
- {
- mmc_io_record s;
- INIT_CPB(s,refnum,MMC_run);
- PBControlAsync((ParmBlkPtr)&s);
- }
-
- void mailcheck::snooze()
- {
- mmc_state s;
- set_item_hilite(fn_snooze,TRUE);
- memset(&s,0,sizeof(s));
- s.mmc_st_maj_ver=MMC_ST_maj_ver;
- s.mmc_st_min_ver=MMC_ST_min_ver;
- if(fs->ref_num==0)
- new_error("The MailCheck driver isn't running so how can it sleep?");
- else
- write_current_state_to_driver(&s);
- set_item_hilite(fn_snooze,FALSE);
- }
-
-
- void mailcheck::applybutton()
- {
- int prep_err;
- int dr_err=0;
- int new_ref;
- set_item_hilite(fn_apply_button,TRUE);
- usernametocur();
- strcpy(fs->cstate.mmc_text_state,"Saved prefrences");
-
- #ifdef RUBBISH
- if((ref_num==0)&&(FLSET(*current_state,MCS_want_open)))
- if((dr_err=mmc_drvr_find_or_install(&new_ref))==0)
- if(OpenDriver(MMC_name,&new_ref)==0)
- ref_num=new_ref;
- #endif
-
- dr_err=0;
- if(fs->ref_num!=0) {
- dr_err=write_current_state_to_driver(&fs->cstate);
- tick(fs->ref_num);
- tick(fs->ref_num);
- tick(fs->ref_num);
- set_display();
- }
- if(dr_err!=0)
- new_error("Can't update driver state");
- else {
- prep_err=mmc_write_prep(&fs->cstate);
- if(prep_err!=0)
- new_error("Can't update MailCheck Pref file");
- }
- set_item_hilite(fn_apply_button,FALSE);
- }
-
- void mailcheck::compute_mmc()
- {
- if((!read_current_state_from_driver(&fs->cstate)) || (mmc_dont_like_prep(&fs->cstate)))
- if((mmc_read_prep(&fs->cstate)!=0) || (mmc_dont_like_prep(&fs->cstate)))
- mmc_invent_state(&fs->cstate);
- set_display();
- }
-
- /*
- * mailcheck::Init
- *
- * A TE record is created to allow entry of mailcheck text. For some
- * reason, editText dialog items don't seem to work in cdev's, so
- * we have to use a userItem. (The item is listed as a statText item
- * in the DITL, giving us a convenient place to store the initial
- * text. We change it to a userItem here.)
- *
- */
- void mailcheck::Init()
- {
-
- inherited::Init();
- fs=(fs_t_pt)NewPtr(sizeof(*fs));
- memset(fs,0,sizeof(*fs));
- fs->init_done=FALSE;
- fs->last_state_update=0;
- fs->old_status= -2;
- init_username_text();
- init_arrows();
-
- find_driver();
- compute_mmc();
- fs->init_done=TRUE;
- }
-
-
- /*
- * mailcheck::Close
- *
- */
-
- void mailcheck::Close()
- {
- TEDispose(fs->hTE);
- DisposPtr((Ptr)fs);
- inherited::Close();
- }
-
-
- /*
- * mailcheck::Update
- *
- */
- void mailcheck::Update()
- {
- Rect box = (**fs->hTE).viewRect;
-
- TEUpdate(&box, fs->hTE);
- InsetRect(&box, -2, -2);
- FrameRect(&box);
-
- if(fs->arrows!=0) {
- Rect draw_where;
- HLock((Handle)fs->arrows);
- draw_where=fs->arrow_box;
- DrawPicture(fs->arrows,&draw_where);
- HUnlock((Handle)fs->arrows);
- }
-
- FrameRect(&fs->error_box);
- FrameRect(&fs->remind_box);
- PenNormal();
- EraseRect(&fs->sound_box);
- FrameRect(&fs->sound_box);
- MoveTo(fs->sound_box.left+1,fs->sound_box.bottom);
- LineTo(fs->sound_box.right,fs->sound_box.bottom);
- LineTo(fs->sound_box.right,fs->sound_box.top+1);
- TextFont(0);
- TextSize(12);
- MoveTo(fs->sound_box.left+14,fs->sound_box.top+12);
- DrawString(fs->sound_text);
- #define REMIND_OFFSET (5)
- PenSize(1,2);
- MoveTo(89,fs->error_box.top-REMIND_OFFSET);
- LineTo(319,fs->error_box.top-REMIND_OFFSET);
- PenNormal();
- }
-
-
- void mailcheck::Activate()
- {
- TEActivate(fs->hTE);
- }
-
-
- void mailcheck::Deactivate()
- {
- TEDeactivate(fs->hTE);
- }
-
- LDEF(long inner_get_named(unsigned char *name))
- {
- return (long)GetNamedResource('snd ',name);
- }
-
- void mailcheck::set_sound(MenuHandle sound_pop,int pick)
- {
- Str255 buf;
- short res_id;
- ResType res_type;
- Handle res_han;
- int prev_res;
- GetItem(sound_pop,pick,buf);
- res_han=(Handle)OSP_protected_call(OSP_noload,inner_get_named,buf);
- if(res_han==0)
- return;
- GetResInfo(res_han,&res_id,&res_type,buf);
- ReleaseResource(res_han);
- fs->cstate.mmc_sound_id=res_id;
- }
-
- int mailcheck::get_sound_menuid(MenuHandle sound_pop)
- {
- int i;
- Str255 buf;
- for(i=CountMItems(sound_pop);i>0;i--) {
- GetItem(sound_pop,i,buf);
- if(memcmp(buf,fs->sound_text,buf[0])==0)
- return i;
- }
- return 0;
- }
-
- void mailcheck::choose_sound()
- {
- #define POP_UP_ID (2312)
- MenuHandle sound_pop;
- int menu_pick;
- Point pop_loc;
- int old_res_file;
- int new_res_file;
- sound_res_state prev_res;
- int def_item;
- pop_loc.v=fs->sound_box.top;
- pop_loc.h=fs->sound_box.left;
- LocalToGlobal(&pop_loc);
- sound_pop=NewMenu(POP_UP_ID,"\pSound Popup");
- if(sound_pop==0) return;
- prev_res=mmc_use_sound_res();
- AddResMenu(sound_pop,'snd ');
- InsertMenu(sound_pop,-1);
- def_item=get_sound_menuid(sound_pop);
- CheckItem(sound_pop,def_item,TRUE);
- menu_pick=PopUpMenuSelect(sound_pop,
- pop_loc.v,pop_loc.h,def_item);
- set_sound(sound_pop,menu_pick);
- DeleteMenu(POP_UP_ID);
- DisposeMenu(sound_pop);
- mmc_close_sound_res(prev_res);
-
- set_sound_display();
- }
-
- void mailcheck::ItemHit(item)
- short item;
- {
- Point where;
-
- switch(item) {
- case fn_user_name:
- where = event->where;
- GlobalToLocal(&where);
- TEClick(where, (event->modifiers & shiftKey) ? true : false, fs->hTE);
- break;
- case fn_last_user_checkbox:
- fs->cstate.mmc_st^=MCS_hearmm;
- set_check_box(fn_last_user_checkbox,FLSET(fs->cstate,MCS_hearmm));
- break;
- case fn_enable_mail_checkbox:
- fs->cstate.mmc_st^=MCS_want_open;
- set_check_box(fn_enable_mail_checkbox,FLSET(fs->cstate,MCS_want_open));
- break;
- case fn_note:
- fs->cstate.mmc_st^=MCS_npost;
- set_check_box(fn_note,FLSET(fs->cstate,MCS_npost));
- break;
- case fn_blink:
- fs->cstate.mmc_st^=MCS_nblink;
- set_check_box(fn_blink,FLSET(fs->cstate,MCS_nblink));
- break;
- case fn_enable_sound:
- fs->cstate.mmc_st^=MCS_nsound;
- set_check_box(fn_enable_sound,FLSET(fs->cstate,MCS_nsound));
- break;
- case fn_sound:
- choose_sound();
- break;
- case fn_apply_button:
- applybutton();
- break;
- case fn_more_remind:
- adjust_remind(figure_step(+1L));
- break;
- case fn_less_remind:
- adjust_remind(figure_step(-1));
- break;
- case fn_snooze:
- snooze();
- break;
- case fn_init_status_text:
- break;
- default:
- break;
- }
- }
-
- void mailcheck::Idle()
- {
- if(!fs->init_done)
- return;
- find_driver_state();
- TEIdle(fs->hTE);
- }
-
-
- void mailcheck::Key(c)
- short c;
- {
- TEKey(c, fs->hTE);
- }
-
- void mailcheck::Cut()
- {
- if ((**fs->hTE).selStart != (**fs->hTE).selEnd)
- TECut(fs->hTE);
- }
-
-
- void mailcheck::Copy()
- {
- if ((**fs->hTE).selStart != (**fs->hTE).selEnd)
- TECopy(fs->hTE);
- }
-
-
- void mailcheck::Paste()
- {
- if (TEScrpLength)
- TEPaste(fs->hTE);
- }
-
-
- void mailcheck::Clear()
- {
- TEDelete(fs->hTE);
- }
-